package com.buba.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.service.SparePartsService;
import common.R;
import entity.sparemanage.SpareParts;
import entity.sparemanage.dto.insertPartsDto;
import entity.sparemanage.dto.requisitionDto;
import entity.sparemanage.dto.selByPartsIdDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.PageResult;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 备件采购 前端控制器
 * </p>
 *
 * @author beisheng
 * @since 2023-05-18
 */
@RestController
@RequestMapping("/spare-parts")
public class SparePartsController {
    @Autowired
    private SparePartsService sparePartsService;

    /* *
     * @Author: beisheng
     * @Descriprion: 查询所有采购单号
     * @Date: 2023/5/26 15:16
     * @Return: common.R
     * @Param: []
     */

    @GetMapping("/selAllSpareParts")
    public R selAllSpareParts(){
        List<SpareParts> spareParts = sparePartsService.list(new QueryWrapper<SpareParts>().eq("delete_flag", 0));
        return R.success(spareParts);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据采购单号查询采购信息和采购人员信息，及供应商信息
     * @Date: 2023/5/26 15:41
     * @Return: common.R
     * @Param: [spId]
     */

    @GetMapping("/selByPartsId/{spId}")
    public R selByPartsId(@PathVariable("spId") Integer spId){
        selByPartsIdDto selByPartsIdDto = sparePartsService.selByPartsId(spId);
        return R.success(selByPartsIdDto);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 分页模糊查询全部采购单
     * @Date: 2023/6/6 9:20
     * @Return: common.R
     * @Param: [requisition]
     */

    @PostMapping("/selAllRequisition")
    public R selAllRequisition(@RequestBody requisitionDto requisition){
        PageResult pageResult = sparePartsService.selAllRequisition(requisition);
        return R.success(pageResult);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 添加备件采购单
     * @Date: 2023/6/12 10:25
     * @Return: common.R
     * @Param: [insertPartsDto]
     */

    @PostMapping("/insertParts")
    public R insertParts(@RequestBody insertPartsDto insertPartsDto){
        sparePartsService.insertParts(insertPartsDto);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 批量逻辑删除采购单
     * @Date: 2023/6/12 14:14
     * @Return: common.R
     * @Param: [ids]
     */

    @PostMapping("/deleteBatchParts")
    public R deleteBatchParts(@RequestBody List<Integer> ids){
        for (Integer id : ids) {
            SpareParts spareParts = new SpareParts();
            spareParts.setDeleteFlag(1);
            spareParts.setUpdateTime(new Date());
            spareParts.setSpId(id);
            sparePartsService.updateById(spareParts);
        }
        return R.success();
    }


}

